package com.sch.example;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.sch.example.action.ActionActivity;
import com.sch.example.checkin.CheckinActivity;
import com.sch.example.util.EmptyUtils;
import com.sch.example.view.CalendarWindow;
import com.sch.example.view.RepeatWindow;
import com.sch.example.view.TimeWindow;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 主页
 */
public class MainActivity extends AppCompatActivity {

    private Button mBtnDate;

    private CalendarWindow mCalendarWindow;
    private TimeWindow mTimeWindow;
    private RepeatWindow mRepeatWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        mBtnDate = findViewById(R.id.btn_date);
        mBtnDate.setOnClickListener(view ->
                mCalendarWindow.showAtLocation(
                        findViewById(R.id.ll_main),
                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0)
        );

        mCalendarWindow = new CalendarWindow(this);
        mCalendarWindow.setOnPositiveListener(v -> {

            // 开始时间
            if (v.getId() == R.id.rl_start_time) {
                mCalendarWindow.dismiss();
                mTimeWindow.showAtLocation(
                        findViewById(R.id.ll_main),
                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

            // 重复设置
            } else if (v.getId() == R.id.rl_repeat) {
                mCalendarWindow.dismiss();
                mRepeatWindow.showAtLocation(
                        findViewById(R.id.ll_main),
                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

            // 提交按钮
            } else if (v.getId() == R.id.rl_submit) {
                mBtnDate.setText(mCalendarWindow.getDate());
                mCalendarWindow.dismiss();
            }

        });
        mTimeWindow = new TimeWindow(this);
        mTimeWindow.setOnBtnListener(view -> {

            // 重置按钮
            if (view.getId() == R.id.tv_re) {
                mCalendarWindow.setStartTime("请选择");

                mTimeWindow.dismiss();

                mCalendarWindow.showAtLocation(
                        findViewById(R.id.ll_main),
                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

                // 确认按钮
            } else if (view.getId() == R.id.tv_ok) {
                if (mTimeWindow.getTimeLeft().equals("不限") || mTimeWindow.getTimeRight().equals("不限")) {
                    mCalendarWindow.setStartTime("不限");
                } else {
                    mCalendarWindow.setStartTime(mTimeWindow.getTimeLeft() + "-" + mTimeWindow.getTimeRight());
                }

                mTimeWindow.dismiss();

                mCalendarWindow.showAtLocation(
                        findViewById(R.id.ll_main),
                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });

        mRepeatWindow = new RepeatWindow(this);
        mRepeatWindow.setOnBtnListener(view -> {

            // 取消按钮
            if (view.getId() == R.id.btn_cancel) {

                mRepeatWindow.dismiss();

                mCalendarWindow.showAtLocation(
                        findViewById(R.id.ll_main),
                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

                // 确认按钮
            } else if (view.getId() == R.id.btn_ok) {

                // 如果是空字符串
                if (EmptyUtils.isEmpty(mRepeatWindow.getWeekString())) {
                    mCalendarWindow.setRepeat("请选择");

                    // 如果是不是空字符串
                } else {

                    // 如果全选了
                    if (mRepeatWindow.getAll_index() == 7) {
                        mCalendarWindow.setRepeat("重复");

                    } else {
                        mCalendarWindow.setRepeat(mRepeatWindow.getWeekString());

                    }

                }

                mRepeatWindow.dismiss();

                mCalendarWindow.showAtLocation(
                        findViewById(R.id.ll_main),
                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            }

        });
    }


    @OnClick(R.id.btn_goto_checkin)
    public void gotoCheckin() {
        startActivity(new Intent(this, CheckinActivity.class));
    }

    @OnClick(R.id.btn_look_action)
    public void lookAction() {
        startActivity(new Intent(this, ActionActivity.class));
    }

}
